package org.mspring.mlog.web.module.admin.bbs;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.bbs.Forum;
import org.mspring.mlog.entity.bbs.ForumCatalog;
import org.mspring.mlog.service.bbs.ForumCatalogService;
import org.mspring.mlog.service.bbs.ForumService;
import org.mspring.mlog.web.module.admin.Admin_AbstractWidget;
import org.mspring.mlog.web.query.bbs.ForumCatalogQueryCriterion;
import org.mspring.mlog.web.query.bbs.ForumQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 论坛
 * 
 * @author Gao Youbo
 * @since 2013年8月6日
 */
@Widget
@RequestMapping("/admin/bbs")
public class Admin_ForumWidget extends Admin_AbstractWidget {

    @Autowired
    private ForumCatalogService forumCatalogService;
    @Autowired
    private ForumService forumService;

    @SuppressWarnings("rawtypes")
    @RequestMapping("/forumCatalog/list")
    public String listForumCatalog(@ModelAttribute Page<ForumCatalog> forumCatalogPage, @ModelAttribute ForumCatalog forumCatalog, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (forumCatalogPage == null) {
            forumCatalogPage = new Page<ForumCatalog>();
        }
        if (forumCatalogPage.getSort() == null) {
            forumCatalogPage.setSort(new Sort("id", Sort.DESC));
        }
        forumCatalogPage = forumCatalogService.listPage(new ForumCatalogQueryCriterion(queryParams), forumCatalogPage);
        model.addAttribute("forumCatalogPage", forumCatalogPage);
        return "/admin/bbs/forumCatalog/listForumCatalog";
    }

    @RequestMapping("/forumCatalog/create")
    public String createForumCatalog(@ModelAttribute ForumCatalog forumCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/bbs/forumCatalog/createForumCatalog";
    }

    @RequestMapping("/forumCatalog/create/save")
    public String createForumCatalog_save(@ModelAttribute ForumCatalog forumCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        forumCatalogService.save(forumCatalog);
        return "redirect:/admin/bbs/forumCatalog/list";
    }

    @RequestMapping("/forumCatalog/edit")
    public String editForumCatalog(@RequestParam(required = false) Long id, @ModelAttribute ForumCatalog forumCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_ForumWidget_editForumCatalog_id");
        }
        SessionUtils.setAttr(request, "Admin_ForumWidget_editForumCatalog_id", id);

        if (id == null) {
            return prompt(model, "请先选择要修改的分类");
        }
        forumCatalog = forumCatalogService.get(id);
        model.addAttribute("forumCatalog", forumCatalog);
        return "/admin/bbs/forumCatalog/editForumCatalog";
    }

    @RequestMapping("/forumCatalog/edit/save")
    public String editForumCatalog_save(@ModelAttribute ForumCatalog forumCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        forumCatalogService.update(forumCatalog);
        return "redirect:/admin/bbs/forumCatalog/list";
    }

    @RequestMapping("/forumCatalog/delete")
    public String deleteForumCatalog(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return prompt(model, "请选择要删除的对象");
        }
        if (forumCatalogService.hasForum(id)) {
            return prompt(model, "无法删除版块分类，该分类下存在版块。");
        }
        forumCatalogService.remove(id);
        return prompt(model, "删除成功", "/admin/bbs/forumCatalog/list");
    }

    @SuppressWarnings("rawtypes")
    @RequestMapping("/forum/list")
    public String listForum(@ModelAttribute Page<Forum> forumPage, @ModelAttribute Forum forum, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (forumPage == null) {
            forumPage = new Page<Forum>();
        }
        if (forumPage.getSort() == null) {
            forumPage.setSort(new Sort("id", Sort.DESC));
        }
        forumPage = forumService.listPage(new ForumQueryCriterion(queryParams), forumPage);
        model.addAttribute("forumPage", forumPage);
        return "/admin/bbs/forum/listForum";
    }

    @RequestMapping("/forum/create")
    public String createForum(@ModelAttribute Forum forum, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("forumCatalogs", forumCatalogService.getAll());
        return "/admin/bbs/forum/createForum";
    }

    @RequestMapping("/forum/create/save")
    public String createForum_save(@ModelAttribute Forum forum, HttpServletRequest request, HttpServletResponse response, Model model) {
        forumService.save(forum);
        return prompt(model, "添加成功", "/admin/bbs/forum/list");
    }

    @RequestMapping("/forum/edit")
    public String editForum(@RequestParam(required = false) Long id, @ModelAttribute Forum forum, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_ForumWidget_editForum_id");
        }
        SessionUtils.setAttr(request, "Admin_ForumWidget_editForum_id", id);

        if (id == null) {
            return prompt(model, "请先选择要修改的版块");
        }
        forum = forumService.get(id);
        model.addAttribute("forum", forum);
        model.addAttribute("forumCatalogs", forumCatalogService.getAll());
        return "/admin/bbs/forum/editForum";
    }

    @RequestMapping("/forum/edit/save")
    public String editForum_save(@ModelAttribute Forum forum, HttpServletRequest request, HttpServletResponse response, Model model) {
        forumService.update(forum);
        return prompt(model, "修改成功", "/admin/bbs/forum/edit?id=" + forum.getId());
    }

    @RequestMapping("/forum/delete")
    public String deleteForum(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return prompt(model, "请确认要删除的版块");
        }
        if (forumService.hasTopic(id)) {
            return prompt(model, "无法删除版块，版块下存在帖子。");
        }
        forumService.remove(id);
        return prompt(model, "删除成功", "/admin/bbs/forum/list");
    }
}
